{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 從課程教材 copy 過來註解"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 基本操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 数组创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "code_folding": [
     0
    ],
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 创建可以将Python，中list列表转换成NumPy数组\n",
    "l = [1,2,3,4,5]\n",
    "\n",
    "# NumPy数组\n",
    "nd1 = np.array(l) # 输入一部分arr + tab（命令中自动补全，按键） 代码提示，自动补全\n",
    "print(nd1)\n",
    "display(nd1) # 显示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0],\n",
       "       [0, 0, 0, 0],\n",
       "       [0, 0, 0, 0]], dtype=int16)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2 = np.zeros(shape = (3,4),dtype = np.int16) # shift + tab提示方法的属性，使用\n",
    "nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.],\n",
       "       [1., 1., 1., 1., 1.]], dtype=float32)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd3 = np.ones(shape = (3,5),dtype=np.float32)\n",
    "nd3 # juppyter中执行程序，代码，最后一行，默认就是输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],\n",
       "\n",
       "       [[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]],\n",
       "\n",
       "       [[3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926],\n",
       "        [3.1415926, 3.1415926, 3.1415926, 3.1415926, 3.1415926]]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 三维数组\n",
    "nd4 = np.full(shape = (3,4,5),fill_value=3.1415926) # 生成任意指定的数组\n",
    "nd4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([88, 68, 55, 25, 95, 45, 53, 52, 94, 48, 38, 41, 12, 66, 18, 38, 15,\n",
       "       53, 15, 86])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd5 = np.random.randint(0,100,size = 20) # 从0，到100，生成随机数字，int，整数\n",
    "nd5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.1805577 , 0.97917756, 0.09591097, 0.07785931, 0.48850737],\n",
       "       [0.89135262, 0.37393773, 0.30382903, 0.75894887, 0.27816675],\n",
       "       [0.75702185, 0.22346079, 0.68415817, 0.40806391, 0.01608321]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd6 = np.random.rand(3,5) # 生成0~1之间随机数\n",
    "nd6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.45444319,  0.91608409, -0.32166309, -1.03094546, -0.46488153],\n",
       "       [-1.31852167,  0.13591764,  0.7220875 ,  1.40014984,  0.18295841],\n",
       "       [-0.22589557, -0.94451934,  1.25952318, -1.72418753,  0.65093392]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nd7 = np.random.randn(3,5) # 正态分布，平均值是0，标准差是1\n",
    "display(nd7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[158.01782738 178.97018664 180.98631405 172.14661692 175.47228488]\n",
      " [174.02104608 182.14529853 146.28615046 183.56698055 173.92432236]\n",
      " [185.93646469 180.11427498 188.36019552 183.88039966 182.85760538]]\n"
     ]
    }
   ],
   "source": [
    "nd8 = np.random.normal(loc = 175,scale = 10,size = (3,5)) # 正态分布，平均值是175，标准差是10\n",
    "print(nd8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1, 11, 21, 31, 41, 51, 61, 71, 81, 91])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd9 = np.arange(1,100,step = 10) # 等差数列，左闭右开，100取不到\n",
    "nd9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1. ,   6.5,  12. ,  17.5,  23. ,  28.5,  34. ,  39.5,  45. ,\n",
       "        50.5,  56. ,  61.5,  67. ,  72.5,  78. ,  83.5,  89. ,  94.5,\n",
       "       100. ])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd10 = np.linspace(1,100,num = 19) # 等差数列，左闭右闭,num表示生成等差数列长度\n",
    "nd10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 查看数组属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.12642702,  0.74807666,  2.42032545],\n",
       "       [-0.27449595, -0.68078987,  0.27759778],\n",
       "       [ 1.15223642, -0.63332573,  0.28521052],\n",
       "       [ 0.39521598,  0.6202027 ,  0.90567908],\n",
       "       [ 0.31221798, -0.19330004, -0.42587603]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "nd = np.random.randn(5,3)\n",
    "nd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 3)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看数组形状，返回了形状 shape = (5,3)\n",
    "nd.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.dtype # 告诉数组的数据类型 float64 位，一位占一个0或者一个1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "15"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.size # 尺寸，数组可以是多维的，请问，里面共有多少数据 3*5 = 15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.ndim # 数组维度 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.itemsize # 条目 尺寸长度 8 字节\n",
    "# 数据类型是float64 64位 -----> 1个字节8位-----> 64/8 = 8 字节"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 文件读写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[22, 32, 86, 19, 24],\n",
       "       [58, 80,  2, 38, 96],\n",
       "       [82, 47, 28, 32, 96]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[-0.04839046,  0.20762731,  1.70801699, -0.41054106,  1.33676572],\n",
       "       [-1.25124635,  0.59099474, -0.74312281,  1.09247369, -0.29597477],\n",
       "       [ 0.90854543,  0.96096231, -1.20553854,  0.9696578 , -0.12783873]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nd1 = np.random.randint(0,100,size = (3,5))\n",
    "nd2 = np.random.randn(3,5)\n",
    "display(nd1,nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "np.save('./data',nd1) # 把一个数据存到文件中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[22, 32, 86, 19, 24],\n",
       "       [58, 80,  2, 38, 96],\n",
       "       [82, 47, 28, 32, 96]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.load('./data.npy') # 默认添加npy后缀"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 多个数据存到一个文件中\n",
    "np.savez('./data.npz',a = nd1,abc = nd2) # 保存数据是起名：a,abc,称为key，自己命名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<numpy.lib.npyio.NpzFile at 0x7fe3d0869910>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.load('./data.npz')\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[22, 32, 86, 19, 24],\n",
       "       [58, 80,  2, 38, 96],\n",
       "       [82, 47, 28, 32, 96]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['a'] # 单引号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.04839046,  0.20762731,  1.70801699, -0.41054106,  1.33676572],\n",
       "       [-1.25124635,  0.59099474, -0.74312281,  1.09247369, -0.29597477],\n",
       "       [ 0.90854543,  0.96096231, -1.20553854,  0.9696578 , -0.12783873]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['abc']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'www is not a file in the archive'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-15-a7937d03f88c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'www'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numpy/lib/npyio.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m    257\u001b[0m                 \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mzip\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    258\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 259\u001b[0;31m             \u001b[0;32mraise\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"%s is not a file in the archive\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    260\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    261\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyError\u001b[0m: 'www is not a file in the archive'"
     ]
    }
   ],
   "source": [
    "data['www'] # 没有保存，无法获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "np.savez_compressed('./data2.npz',x = nd1,y = nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[22, 32, 86, 19, 24],\n",
       "       [58, 80,  2, 38, 96],\n",
       "       [82, 47, 28, 32, 96]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.load('./data2.npz')['x']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "np.savetxt(fname = './data.txt',# 文件名\n",
    "           X = nd1, # 数据\n",
    "           fmt='%0.2f', # 格式\n",
    "           delimiter=',')# 分隔符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "np.savetxt(fname = './data.cvs',# 文件名\n",
    "           X = nd1, # 数据\n",
    "           fmt='%d', # 格式\n",
    "           delimiter=';')# 分隔符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[22., 32., 86., 19., 24.],\n",
       "       [58., 80.,  2., 38., 96.],\n",
       "       [82., 47., 28., 32., 96.]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.loadtxt('./data.cvs',delimiter=';')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[22., 32., 86., 19., 24.],\n",
       "       [58., 80.,  2., 38., 96.],\n",
       "       [82., 47., 28., 32., 96.]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.loadtxt('./data.txt',delimiter=',')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4, 7], dtype=int8)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# int8，int16，int32，int64，uint8无符号\n",
    "# float16，float32，float64\n",
    "# str字符串类型\n",
    "# int8 表示 2**8个数字 256个 -128 ~ 127 有符号\n",
    "# uint8 表示256个数字，无符号，表明只有正数：0 ~ 255\n",
    "\n",
    "np.array([2,4,7],dtype = np.int8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([253, 249, 255, 108,   0,   0], dtype=uint8)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([-3,-7,255,108,0,256],dtype = np.uint8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([59,  9, 31, 47, 90, 84, 89, 10, 85, 13])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0,100,size = 10,dtype = 'int64')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.08697582, 0.01856716],\n",
       "       [0.28368212, 0.01316251],\n",
       "       [0.20035117, 0.6809994 ],\n",
       "       [0.31315228, 0.40969755],\n",
       "       [0.79286082, 0.24707183],\n",
       "       [0.60774558, 0.32297674],\n",
       "       [0.54115188, 0.89123677],\n",
       "       [0.99941816, 0.15384657],\n",
       "       [0.52934283, 0.61635984],\n",
       "       [0.34174069, 0.30613281]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd = np.random.rand(10,2)\n",
    "nd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.087  , 0.01857],\n",
       "       [0.2837 , 0.01316],\n",
       "       [0.2003 , 0.681  ],\n",
       "       [0.3132 , 0.4097 ],\n",
       "       [0.793  , 0.2471 ],\n",
       "       [0.608  , 0.323  ],\n",
       "       [0.541  , 0.891  ],\n",
       "       [0.9995 , 0.1538 ],\n",
       "       [0.5293 , 0.616  ],\n",
       "       [0.3418 , 0.3062 ]], dtype=float16)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.asarray(nd,dtype = 'float16')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.087  , 0.01857],\n",
       "       [0.2837 , 0.01316],\n",
       "       [0.2003 , 0.681  ],\n",
       "       [0.3132 , 0.4097 ],\n",
       "       [0.793  , 0.2471 ],\n",
       "       [0.608  , 0.323  ],\n",
       "       [0.541  , 0.891  ],\n",
       "       [0.9995 , 0.1538 ],\n",
       "       [0.5293 , 0.616  ],\n",
       "       [0.3418 , 0.3062 ]], dtype=float16)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd.astype(dtype = np.float16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "nd = np.random.randn(1000,3) # 默认数据类型是float64\n",
    "np.save('./data1',nd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "np.save('./data2',nd.astype('float16'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'], dtype='<U1')"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2 = np.array(list('abcdefghi'))\n",
    "nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('<U1')"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 数组运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 基本运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 8, 6, 8, 9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([4, 4, 9, 5, 7])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 加减乘除指数幂运算\n",
    "nd1 = np.random.randint(0,10,size = 5)\n",
    "nd2 = np.random.randint(0,10,size = 5)\n",
    "display(nd1,nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-380, -380, -391, -381, -380])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd3 = nd1 - nd2 # 返回一个新对象，原来的数组，内容不变！\n",
    "nd3 # nd3数组操作后，接收的对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([32, 32, 54, 40, 63])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 * nd2 # 乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.        , 2.        , 0.66666667, 1.6       , 1.28571429])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 / nd2 # 除法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([    4096,     4096, 10077696,    32768,  4782969])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1**nd2 # 幂运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2**3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.power(2,3) # 表示2的3次幂"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([    4096,     4096, 10077696,    32768,  4782969])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.power(nd1,nd2) # 表示nd1的nd2次幂，对应位置，进行计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.605170185988092"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log(100) # 底数是自然底数e 2.718"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log10(1000) # 对数运算返回结果是：3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10.0"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log2(1024) # 返回结果就是：10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 逻辑运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 8, 6, 8, 9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([4, 4, 9, 5, 7])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(nd1,nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True, False,  True,  True])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 > nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False,  True, False, False])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 < nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True, False,  True,  True])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 >= nd2 # 表示nd1数组中的数据，是否大于等于nd2中的对应位置的数据，如果大于等于，放回True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False, False])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 == nd2 # 两个等号表示逻辑判断，问，是否相等"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 数组与标量计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 8, 6, 8, 9])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([18, 18, 16, 18, 19])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数字3，4，5……都是标量\n",
    "nd1 + 10 # 所有的位置都加了10，广播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1016, -1016, -1018, -1016, -1015])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 - 1024"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2048, 2048, 1536, 2048, 2304])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 * 256"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.0078125 , 0.0078125 , 0.00585938, 0.0078125 , 0.00878906])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 / 1024"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.125     , 0.125     , 0.16666667, 0.125     , 0.11111111])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组可以做分母，注意不能有0\n",
    "1/nd1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ipykernel_launcher.py:1: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([1.        , 0.33333333,        inf, 0.2       ])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1/np.array([1,3,0,5]) # 0不能作为分母计算结果：inf "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "### -= += *=直接改变原数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 8, 6, 8, 9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([4, 4, 9, 5, 7])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(nd1,nd2) # 没变化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "nd1 -= 100 # 没有打印输出，说明，改变了原来的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-92, -92, -94, -92, -91])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([104, 104, 109, 105, 107])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2 +=100\n",
    "nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-276, -276, -282, -276, -273])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 *= 3\n",
    "nd1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "code_folding": [],
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "No loop matching the specified signature and casting was found for ufunc true_divide",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-69-aebfed68561c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnd1\u001b[0m \u001b[0;34m/=\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: No loop matching the specified signature and casting was found for ufunc true_divide"
     ]
    }
   ],
   "source": [
    "# nd1 /= 10 数组不支持 /="
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 复制和视图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 完全没有复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 9, 5, 3, 8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([3, 9, 5, 3, 8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(0,10,size = 5)\n",
    "b = a # 赋值操作\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a is b # 返回True说明，赋值操作，a和b一回事"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1024,    9,    5,    3,    8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1024,    9,    5,    3,    8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a[0] = 1024 # 改变a那么b也发生了变化\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 视图、查看或者浅拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0, 86, 29, 73, 38])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([ 0, 86, 29, 73, 38])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(0,100,size = 5)\n",
    "b = a.view() # 视图，查看，浅拷贝\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a is b # 说明a和b不一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flags.owndata # a数组数据是自己的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.flags.owndata # b是浅拷贝a的数据，也就是b并不拥有自己的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1024, 2048,   29,   73,   38])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1024, 2048,   29,   73,   38])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a[0] = 1024\n",
    "b[1] = 2048 # 无论修改谁，最终结果两个数组都发生了变化\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "### 深拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-38, -74, -18, -30,  -1, -81,  -5, -42, -44, -12])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([-38, -74, -18, -30,  -1, -81,  -5, -42, -44, -12])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(-100,0,size = 10)\n",
    "b = a.copy() # 深拷贝，此时，a和b没有关系了\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(a is b)\n",
    "display(a.flags.owndata)\n",
    "display(b.flags.owndata) # b 对象拥有自己的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1024,  -74,  -18,  -30,   -1,  -81,   -5,  -42,  -44,  -12])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([ -38,  -74, 2048,  -30,   -1,  -81,   -5,  -42,  -44,  -12])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a[0] = 1024\n",
    "b[2] = 2048 # 井水不犯河水\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.0000000e+00, 1.0000000e+00, 2.0000000e+00, ..., 9.9999997e+07,\n",
       "       9.9999998e+07, 9.9999999e+07])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(1e8) # 0 ~ 1亿，数据量非常多的\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "b = a[[1,3,5,7,9,99]].copy() # 取出一部分数据，原来的数组，没有了，但是占内存特别大\n",
    "del a # 删除原来的数组，内存优化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.,  3.,  5.,  7.,  9., 99.])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 索引、切片和迭代"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 基本索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 7,  5, 27,  7, 29, 11, 29,  3, 23, 23])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,30,size = 10)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5,  7, 11])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[3] # 取一个\n",
    "a[[1,3,5]] # 取多个 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 7,  5, 27])"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0:3] # 左闭右开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 7,  5, 27])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:3] # 如果冒号前面不写，默认从0开始"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11, 29,  3, 23])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[5:9] # 从某个索引开始切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11, 29,  3, 23, 23])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[5:] # 冒号后面不写内容，那么默认就是到左后"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 7, 27, 29, 29, 23])"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::2] # 两个中取一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 7, 29, 23])"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[3::3] # 从索引3开始，每三个数中，取一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([23, 23,  3, 29, 11, 29,  7, 27,  5,  7])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::-1] # 倒着数，数组进行了颠倒"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 7,  5, 27,  7, 29, 11, 29,  3, 23, 23])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([23,  3, 11,  7,  5])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::-2] # 颠倒，两个中取一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11, 27])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[5::-3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5,  7, 11])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1:7:2] # 从索引1开始到7结束，每两个中取一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[29, 17,  4, 23,  4, 12, 18, 22,  3,  2],\n",
       "       [18, 15, 19, 23, 17,  6, 29, 15, 22, 18],\n",
       "       [10, 20, 16, 11, 18,  3, 10, 20, 16, 28],\n",
       "       [10,  0,  9, 11,  7, 27, 26, 27, 29, 25],\n",
       "       [21,  0,  1, 16,  4, 11,  9, 15, 29, 12],\n",
       "       [15,  3, 29, 26, 17, 15, 22, 23,  8,  3],\n",
       "       [ 3,  1, 25,  9, 15, 22,  5, 15, 15, 29],\n",
       "       [ 7,  9, 14, 29, 28,  4,  8, 28,  5, 18],\n",
       "       [ 0,  2, 19, 10, 18,  0,  9, 16, 10, 19],\n",
       "       [ 5, 29,  6, 24,  1,  4, 19, 29, 14, 27]])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.random.randint(0,30,size = (10,10))\n",
    "b # 二维数组，多维数据索引和切片和上面的规律一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([18, 15, 19, 23, 17,  6, 29, 15, 22, 18])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[29, 17,  4, 23,  4, 12, 18, 22,  3,  2],\n",
       "       [10,  0,  9, 11,  7, 27, 26, 27, 29, 25],\n",
       "       [15,  3, 29, 26, 17, 15, 22, 23,  8,  3]])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[[0,3,5]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "29"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[1,6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9, 27, 26])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[3,[2,5,6]] # 多维数组，不怕，我们可以用逗号，分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[20, 18, 20],\n",
       "       [ 0,  7, 27],\n",
       "       [ 0,  4, 15],\n",
       "       [ 3, 17, 23],\n",
       "       [ 1, 15, 15]])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[2:7,1::3] # 行：从2到索引7。列：从1开始，每3个中取一个数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[-1,-1] # 给-1表示倒着数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 16,  9])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[-2,[-2,-3,-4]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "### 花式索引和索引技巧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "       17, 18, 19])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(20)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4, 5, 6])"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a[3:7] # 切片时，返回的数据，不是深拷贝\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   0,    1,    2, 1024,    4,    5,    6,    7,    8,    9,   10,\n",
       "         11,   12,   13,   14,   15,   16,   17,   18,   19])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1024,    4,    5,    6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "b[0] = 1024\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "       17, 18, 19])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([3, 4, 5, 6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.arange(20)\n",
    "# 花式索引返回的深拷贝的数据\n",
    "b = a[[3,4,5,6]] # 花式索引：就是在索引是，给了一个数组\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "       17, 18, 19])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1024,    4,    5,    6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "b[0] = 1024\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[117, 127, 112],\n",
       "       [150, 133, 147],\n",
       "       [104, 114, 123],\n",
       "       [ 63,  60, 108],\n",
       "       [111,  18, 121],\n",
       "       [137,  67,  81],\n",
       "       [ 14, 125,  38],\n",
       "       [ 72, 123, 143],\n",
       "       [132,  13, 114],\n",
       "       [ 91, 108, 136],\n",
       "       [ 41,  49,  72],\n",
       "       [ 79, 148,  20],\n",
       "       [ 23, 114,  18],\n",
       "       [146, 144, 125],\n",
       "       [ 76,  34,  76],\n",
       "       [134,  45,  91],\n",
       "       [ 62,  13, 122],\n",
       "       [ 22,  74,  52],\n",
       "       [103, 124,  96],\n",
       "       [ 50, 134, 137],\n",
       "       [ 82, 126,  23],\n",
       "       [ 76, 122, 116],\n",
       "       [116,  11, 115],\n",
       "       [115,  15,  10],\n",
       "       [ 12,  31,   4],\n",
       "       [ 14, 134,  57],\n",
       "       [ 83, 132,  84],\n",
       "       [118,  25,  49],\n",
       "       [109, 123,  63],\n",
       "       [130, 106, 126],\n",
       "       [ 25, 145, 103],\n",
       "       [ 67,  96,  74],\n",
       "       [ 84,  52,  78],\n",
       "       [ 54,  79, 137],\n",
       "       [ 54, 127,  48],\n",
       "       [ 31,  22,  39],\n",
       "       [150, 100,  84],\n",
       "       [122, 123,  14],\n",
       "       [125,  12,  51],\n",
       "       [  6,   0,   9],\n",
       "       [ 25,  18,  42],\n",
       "       [ 19,  74,   2],\n",
       "       [ 43, 127,   6],\n",
       "       [ 64, 137, 110],\n",
       "       [ 13,  59, 133],\n",
       "       [ 51, 122,  15],\n",
       "       [ 58,  65,  28],\n",
       "       [109,  38,  49],\n",
       "       [114, 133,  90],\n",
       "       [  2, 144, 125],\n",
       "       [130,  55, 144],\n",
       "       [ 11,  98,  45],\n",
       "       [118, 105, 120],\n",
       "       [122,  19,  39],\n",
       "       [  8,  80,  56],\n",
       "       [ 36,  51,  81],\n",
       "       [104,  10,  30],\n",
       "       [113, 101,  57],\n",
       "       [  7,  96,  50],\n",
       "       [ 73, 127, 126],\n",
       "       [ 32, 124,  53],\n",
       "       [ 93,  64,  65],\n",
       "       [ 16, 121,  71],\n",
       "       [ 12,  30,  61],\n",
       "       [112,  34,  33],\n",
       "       [ 66,   9,  95],\n",
       "       [ 24,  68,  55],\n",
       "       [ 16,   7, 148],\n",
       "       [ 14, 127,  55],\n",
       "       [ 45,  97,  22],\n",
       "       [ 72,  53, 103],\n",
       "       [ 41,  59, 142],\n",
       "       [ 26,  89,  47],\n",
       "       [116, 139, 136],\n",
       "       [  3,  25,  18],\n",
       "       [121, 140,  61],\n",
       "       [135,  36,  12],\n",
       "       [130,  32, 125],\n",
       "       [112, 113, 135],\n",
       "       [ 39, 100, 140],\n",
       "       [ 23,  68,  54],\n",
       "       [136, 131,   5],\n",
       "       [ 23,   0,  87],\n",
       "       [138,  97, 147],\n",
       "       [ 57,   9,   3],\n",
       "       [ 59,  92,  50],\n",
       "       [ 36,  38,  22],\n",
       "       [113,  74,   2],\n",
       "       [ 50,  85,   8],\n",
       "       [113,  89,   4],\n",
       "       [112,  33,  51],\n",
       "       [ 31,  23,  74],\n",
       "       [103,  67,  53],\n",
       "       [ 14, 119, 140],\n",
       "       [107,  26,  83],\n",
       "       [ 21, 148, 140],\n",
       "       [ 88, 124,  87],\n",
       "       [ 95,  89,  49],\n",
       "       [ 77,   4, 106],\n",
       "       [ 52, 138,   8]])"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,151,size = (100,3)) # 100名学生，参加了3门考试:Python、Math、En\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([127, 150, 133, 147, 123, 121, 137, 125, 123, 143, 132, 136, 148,\n",
       "       146, 144, 125, 134, 122, 124, 134, 137, 126, 122, 134, 132, 123,\n",
       "       130, 126, 145, 137, 127, 150, 122, 123, 125, 127, 137, 133, 122,\n",
       "       133, 144, 125, 130, 144, 120, 122, 127, 126, 124, 121, 148, 127,\n",
       "       142, 139, 136, 121, 140, 135, 130, 125, 135, 140, 136, 131, 138,\n",
       "       147, 140, 148, 140, 124, 138])"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond = a >= 120 # 逻辑运算\n",
    "# 根据条件，筛选数据，只要大于120，返回，一门大于120，就会返回这一门\n",
    "a[cond]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[150, 133, 147],\n",
       "       [146, 144, 125]])"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# boolean True = 1;False = 0\n",
    "# 三门科目的条件进行相乘\n",
    "# 三个科目都是 大于120的同学\n",
    "cond2 = cond[:,0]*cond[:,1]*cond[:,2]\n",
    "a[cond2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6,  0,  9],\n",
       "       [ 3, 25, 18]])"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 大于等于120，小于等于30找到\n",
    "cond1 = a >=120\n",
    "cond2 = a <= 30\n",
    "a[cond2[:,0]*cond2[:,1]*cond2[:,2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 形状操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 数组变形"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 1, 8, 2, 7],\n",
       "       [2, 2, 5, 1, 9],\n",
       "       [9, 2, 6, 7, 0]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,10,size = (3,5))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 1, 8],\n",
       "       [2, 7, 2],\n",
       "       [2, 5, 1],\n",
       "       [9, 9, 2],\n",
       "       [6, 7, 0]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(5,3) # 只是改变形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[5]],\n",
       "\n",
       "       [[1]],\n",
       "\n",
       "       [[8]],\n",
       "\n",
       "       [[2]],\n",
       "\n",
       "       [[7]],\n",
       "\n",
       "       [[2]],\n",
       "\n",
       "       [[2]],\n",
       "\n",
       "       [[5]],\n",
       "\n",
       "       [[1]],\n",
       "\n",
       "       [[9]],\n",
       "\n",
       "       [[9]],\n",
       "\n",
       "       [[2]],\n",
       "\n",
       "       [[6]],\n",
       "\n",
       "       [[7]],\n",
       "\n",
       "       [[0]]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(15,1,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 1, 8],\n",
       "       [2, 7, 2],\n",
       "       [2, 5, 1],\n",
       "       [9, 9, 2],\n",
       "       [6, 7, 0]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.reshape(-1,3) # -1表示数据，3自动计算-1 = 5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 数组转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 2, 9],\n",
       "       [1, 2, 2],\n",
       "       [8, 5, 6],\n",
       "       [2, 1, 7],\n",
       "       [7, 9, 0]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转置，行变列，列变行\n",
    "a.T # 矩阵 shape = (5,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 2, 9],\n",
       "       [1, 2, 2],\n",
       "       [8, 5, 6],\n",
       "       [2, 1, 7],\n",
       "       [7, 9, 0]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.transpose(a, (0, 1)) # 跟原本一樣沒變\n",
    "\n",
    "np.transpose(a,(1,0)) # 和 a.T 結果一樣\n",
    "#行0，列1。默认情况下(0,1)----->调整(1,0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 3, 9, 0, 4, 6, 9],\n",
       "        [5, 8, 0, 5, 0, 6, 9],\n",
       "        [5, 1, 2, 2, 8, 5, 7],\n",
       "        [3, 0, 6, 4, 2, 1, 8],\n",
       "        [8, 6, 5, 7, 2, 6, 6]],\n",
       "\n",
       "       [[9, 5, 4, 1, 4, 2, 2],\n",
       "        [4, 5, 6, 3, 5, 6, 0],\n",
       "        [9, 5, 1, 0, 2, 4, 1],\n",
       "        [0, 2, 5, 0, 1, 1, 1],\n",
       "        [7, 8, 4, 8, 9, 2, 6]],\n",
       "\n",
       "       [[5, 9, 7, 3, 5, 5, 2],\n",
       "        [4, 8, 7, 7, 9, 2, 5],\n",
       "        [2, 7, 0, 6, 1, 9, 5],\n",
       "        [1, 7, 2, 4, 1, 0, 6],\n",
       "        [6, 9, 6, 0, 5, 3, 5]]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.random.randint(0,10,size = (3,5,7)) # shape = (0,1,2)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 5, 5, 3, 8],\n",
       "        [3, 8, 1, 0, 6],\n",
       "        [9, 0, 2, 6, 5],\n",
       "        [0, 5, 2, 4, 7],\n",
       "        [4, 0, 8, 2, 2],\n",
       "        [6, 6, 5, 1, 6],\n",
       "        [9, 9, 7, 8, 6]],\n",
       "\n",
       "       [[9, 4, 9, 0, 7],\n",
       "        [5, 5, 5, 2, 8],\n",
       "        [4, 6, 1, 5, 4],\n",
       "        [1, 3, 0, 0, 8],\n",
       "        [4, 5, 2, 1, 9],\n",
       "        [2, 6, 4, 1, 2],\n",
       "        [2, 0, 1, 1, 6]],\n",
       "\n",
       "       [[5, 4, 2, 1, 6],\n",
       "        [9, 8, 7, 7, 9],\n",
       "        [7, 7, 0, 2, 6],\n",
       "        [3, 7, 6, 4, 0],\n",
       "        [5, 9, 1, 1, 5],\n",
       "        [5, 2, 9, 0, 3],\n",
       "        [2, 5, 5, 6, 5]]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.transpose(b,(2,1,0)) # 就是调整维度结构2和0维度数据对调\n",
    "c = np.transpose(b,(0,2,1)) # 0, 1, 2 三種調換 代表第一維、第二維、第三維調換\n",
    "c "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 数据堆叠合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 4, 5, 3, 5],\n",
       "       [6, 7, 5, 2, 3],\n",
       "       [7, 3, 6, 7, 6]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[3, 5, 7, 6, 3],\n",
       "       [7, 8, 4, 1, 5],\n",
       "       [6, 3, 9, 1, 6]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nd1 = np.random.randint(0,10,size = (3,5))\n",
    "nd2 = np.random.randint(0,10,size = (3,5))\n",
    "display(nd1,nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 4, 5, 3, 5],\n",
       "       [6, 7, 5, 2, 3],\n",
       "       [7, 3, 6, 7, 6],\n",
       "       [3, 5, 7, 6, 3],\n",
       "       [7, 8, 4, 1, 5],\n",
       "       [6, 3, 9, 1, 6]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([nd1,nd2]) # 默认往 axis =0 方向合并行增加 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 4, 5, 3, 5, 3, 5, 7, 6, 3],\n",
       "       [6, 7, 5, 2, 3, 7, 8, 4, 1, 5],\n",
       "       [7, 3, 6, 7, 6, 6, 3, 9, 1, 6]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改axis参数调整数据合并方向\n",
    "np.concatenate([nd1,nd2], axis = 1) # axis 轴，方向 0 = 行，1 = 列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 4, 5, 3, 5, 3, 5, 7, 6, 3],\n",
       "       [6, 7, 5, 2, 3, 7, 8, 4, 1, 5],\n",
       "       [7, 3, 6, 7, 6, 6, 3, 9, 1, 6]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((nd1,nd2)) # 堆叠，摞起来，增多，合并 h表示水平，列增多 h: axis=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 4, 5, 3, 5],\n",
       "       [6, 7, 5, 2, 3],\n",
       "       [7, 3, 6, 7, 6],\n",
       "       [3, 5, 7, 6, 3],\n",
       "       [7, 8, 4, 1, 5],\n",
       "       [6, 3, 9, 1, 6],\n",
       "       [3, 5, 7, 6, 3],\n",
       "       [7, 8, 4, 1, 5],\n",
       "       [6, 3, 9, 1, 6],\n",
       "       [3, 5, 7, 6, 3],\n",
       "       [7, 8, 4, 1, 5],\n",
       "       [6, 3, 9, 1, 6],\n",
       "       [7, 4, 5, 3, 5],\n",
       "       [6, 7, 5, 2, 3],\n",
       "       [7, 3, 6, 7, 6]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((nd1,nd2,nd2,nd2,nd1)) # v 竖直，行增加  v: axis=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 6, 6, 6, 9],\n",
       "       [4, 2, 0, 5, 2],\n",
       "       [9, 6, 3, 1, 4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[5, 2, 8, 1, 6, 9],\n",
       "       [1, 0, 4, 2, 9, 4],\n",
       "       [1, 4, 4, 3, 9, 8]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "ename": "ValueError",
     "evalue": "all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 5 and the array at index 1 has size 6",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-26-f248ef84396b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrandint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0msize\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mdisplay\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconcatenate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mb\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m  \u001b[0;31m# axis=0 方向無法合併因為 shape[1] 不同\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<__array_function__ internals>\u001b[0m in \u001b[0;36mconcatenate\u001b[0;34m(*args, **kwargs)\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 5 and the array at index 1 has size 6"
     ]
    }
   ],
   "source": [
    "a = np.random.randint(0,10,size = (3,5))\n",
    "b = np.random.randint(0,10,size = (3,6))\n",
    "display(a,b)\n",
    "np.concatenate([a,b])  # axis=0 方向無法合併因為 shape[1] 不同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 6, 6, 6, 9, 5, 2, 8, 1, 6, 9],\n",
       "       [4, 2, 0, 5, 2, 1, 0, 4, 2, 9, 4],\n",
       "       [9, 6, 3, 1, 4, 1, 4, 4, 3, 9, 8]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([a,b],axis =1)  # 改為 axis=1 方向合併"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### [数组拆分](https://jd.com)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[63, 22, 73, 36, 48, 83, 85, 20, 75, 88],\n",
       "       [24, 68, 44, 69, 17, 99, 21, 82,  5, 30],\n",
       "       [32, 70, 21, 20, 19, 41, 21, 15, 34, 68],\n",
       "       [19, 26, 66, 54,  1, 44, 49, 56, 12, 86],\n",
       "       [64, 34, 61, 13,  4, 76, 91, 89, 21, 15],\n",
       "       [63, 50, 40,  0, 96,  6, 23,  3,  1, 58],\n",
       "       [ 7, 79, 96, 16, 29, 85, 73,  0, 88, 17],\n",
       "       [81,  7, 95, 50, 79, 61,  2, 33, 97, 14],\n",
       "       [42, 58, 75, 16, 72, 30, 46, 67, 87, 75],\n",
       "       [ 4, 62,  9, 12, 81, 53,  0,  1,  3, 51],\n",
       "       [28, 52, 43, 55, 84, 30,  8, 78, 73, 77],\n",
       "       [90, 49, 86, 62,  1, 44, 51, 65, 89,  5],\n",
       "       [15, 77, 24,  6, 77, 22,  1, 69, 69, 18],\n",
       "       [74, 64, 76, 82, 49, 48,  9, 11, 10, 29],\n",
       "       [45, 30,  9, 16, 70, 64, 92, 68, 61,  3]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,100,size = (15,10))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[63, 22, 73, 36, 48, 83, 85, 20, 75, 88],\n",
       "        [24, 68, 44, 69, 17, 99, 21, 82,  5, 30],\n",
       "        [32, 70, 21, 20, 19, 41, 21, 15, 34, 68]]),\n",
       " array([[19, 26, 66, 54,  1, 44, 49, 56, 12, 86],\n",
       "        [64, 34, 61, 13,  4, 76, 91, 89, 21, 15],\n",
       "        [63, 50, 40,  0, 96,  6, 23,  3,  1, 58]]),\n",
       " array([[ 7, 79, 96, 16, 29, 85, 73,  0, 88, 17],\n",
       "        [81,  7, 95, 50, 79, 61,  2, 33, 97, 14],\n",
       "        [42, 58, 75, 16, 72, 30, 46, 67, 87, 75]]),\n",
       " array([[ 4, 62,  9, 12, 81, 53,  0,  1,  3, 51],\n",
       "        [28, 52, 43, 55, 84, 30,  8, 78, 73, 77],\n",
       "        [90, 49, 86, 62,  1, 44, 51, 65, 89,  5]]),\n",
       " array([[15, 77, 24,  6, 77, 22,  1, 69, 69, 18],\n",
       "        [74, 64, 76, 82, 49, 48,  9, 11, 10, 29],\n",
       "        [45, 30,  9, 16, 70, 64, 92, 68, 61,  3]])]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a, indices_or_sections=5) # 给数字，表示平均分成多少分（需能整除），預設 axis=0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[63, 22, 73, 36, 48],\n",
       "        [24, 68, 44, 69, 17],\n",
       "        [32, 70, 21, 20, 19],\n",
       "        [19, 26, 66, 54,  1],\n",
       "        [64, 34, 61, 13,  4],\n",
       "        [63, 50, 40,  0, 96],\n",
       "        [ 7, 79, 96, 16, 29],\n",
       "        [81,  7, 95, 50, 79],\n",
       "        [42, 58, 75, 16, 72],\n",
       "        [ 4, 62,  9, 12, 81],\n",
       "        [28, 52, 43, 55, 84],\n",
       "        [90, 49, 86, 62,  1],\n",
       "        [15, 77, 24,  6, 77],\n",
       "        [74, 64, 76, 82, 49],\n",
       "        [45, 30,  9, 16, 70]]),\n",
       " array([[83, 85, 20, 75, 88],\n",
       "        [99, 21, 82,  5, 30],\n",
       "        [41, 21, 15, 34, 68],\n",
       "        [44, 49, 56, 12, 86],\n",
       "        [76, 91, 89, 21, 15],\n",
       "        [ 6, 23,  3,  1, 58],\n",
       "        [85, 73,  0, 88, 17],\n",
       "        [61,  2, 33, 97, 14],\n",
       "        [30, 46, 67, 87, 75],\n",
       "        [53,  0,  1,  3, 51],\n",
       "        [30,  8, 78, 73, 77],\n",
       "        [44, 51, 65, 89,  5],\n",
       "        [22,  1, 69, 69, 18],\n",
       "        [48,  9, 11, 10, 29],\n",
       "        [64, 92, 68, 61,  3]])]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(a,indices_or_sections=2, axis =1) # axis = 1 表示列，平均分成两份"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[63, 22, 73, 36, 48, 83, 85, 20, 75, 88]]),\n",
       " array([[24, 68, 44, 69, 17, 99, 21, 82,  5, 30],\n",
       "        [32, 70, 21, 20, 19, 41, 21, 15, 34, 68],\n",
       "        [19, 26, 66, 54,  1, 44, 49, 56, 12, 86],\n",
       "        [64, 34, 61, 13,  4, 76, 91, 89, 21, 15]]),\n",
       " array([[63, 50, 40,  0, 96,  6, 23,  3,  1, 58],\n",
       "        [ 7, 79, 96, 16, 29, 85, 73,  0, 88, 17],\n",
       "        [81,  7, 95, 50, 79, 61,  2, 33, 97, 14],\n",
       "        [42, 58, 75, 16, 72, 30, 46, 67, 87, 75]]),\n",
       " array([[ 4, 62,  9, 12, 81, 53,  0,  1,  3, 51],\n",
       "        [28, 52, 43, 55, 84, 30,  8, 78, 73, 77],\n",
       "        [90, 49, 86, 62,  1, 44, 51, 65, 89,  5],\n",
       "        [15, 77, 24,  6, 77, 22,  1, 69, 69, 18],\n",
       "        [74, 64, 76, 82, 49, 48,  9, 11, 10, 29],\n",
       "        [45, 30,  9, 16, 70, 64, 92, 68, 61,  3]])]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 参数给列表，根据列表中的索引，进行切片\n",
    "np.split(a, indices_or_sections=[1,5,9]) # 0~1,1~5,5~9,9~"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[63, 22, 73, 36, 48],\n",
       "        [24, 68, 44, 69, 17],\n",
       "        [32, 70, 21, 20, 19],\n",
       "        [19, 26, 66, 54,  1],\n",
       "        [64, 34, 61, 13,  4],\n",
       "        [63, 50, 40,  0, 96],\n",
       "        [ 7, 79, 96, 16, 29],\n",
       "        [81,  7, 95, 50, 79],\n",
       "        [42, 58, 75, 16, 72],\n",
       "        [ 4, 62,  9, 12, 81],\n",
       "        [28, 52, 43, 55, 84],\n",
       "        [90, 49, 86, 62,  1],\n",
       "        [15, 77, 24,  6, 77],\n",
       "        [74, 64, 76, 82, 49],\n",
       "        [45, 30,  9, 16, 70]]),\n",
       " array([[83, 85, 20, 75, 88],\n",
       "        [99, 21, 82,  5, 30],\n",
       "        [41, 21, 15, 34, 68],\n",
       "        [44, 49, 56, 12, 86],\n",
       "        [76, 91, 89, 21, 15],\n",
       "        [ 6, 23,  3,  1, 58],\n",
       "        [85, 73,  0, 88, 17],\n",
       "        [61,  2, 33, 97, 14],\n",
       "        [30, 46, 67, 87, 75],\n",
       "        [53,  0,  1,  3, 51],\n",
       "        [30,  8, 78, 73, 77],\n",
       "        [44, 51, 65, 89,  5],\n",
       "        [22,  1, 69, 69, 18],\n",
       "        [48,  9, 11, 10, 29],\n",
       "        [64, 92, 68, 61,  3]])]"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(a,indices_or_sections=2) # h水平，列方向上分割成了两份 h: axis=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[63, 22, 73, 36, 48, 83, 85, 20, 75, 88],\n",
       "        [24, 68, 44, 69, 17, 99, 21, 82,  5, 30],\n",
       "        [32, 70, 21, 20, 19, 41, 21, 15, 34, 68]]),\n",
       " array([[19, 26, 66, 54,  1, 44, 49, 56, 12, 86],\n",
       "        [64, 34, 61, 13,  4, 76, 91, 89, 21, 15],\n",
       "        [63, 50, 40,  0, 96,  6, 23,  3,  1, 58],\n",
       "        [ 7, 79, 96, 16, 29, 85, 73,  0, 88, 17]]),\n",
       " array([[81,  7, 95, 50, 79, 61,  2, 33, 97, 14],\n",
       "        [42, 58, 75, 16, 72, 30, 46, 67, 87, 75],\n",
       "        [ 4, 62,  9, 12, 81, 53,  0,  1,  3, 51],\n",
       "        [28, 52, 43, 55, 84, 30,  8, 78, 73, 77]]),\n",
       " array([[90, 49, 86, 62,  1, 44, 51, 65, 89,  5],\n",
       "        [15, 77, 24,  6, 77, 22,  1, 69, 69, 18],\n",
       "        [74, 64, 76, 82, 49, 48,  9, 11, 10, 29],\n",
       "        [45, 30,  9, 16, 70, 64, 92, 68, 61,  3]])]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(a,indices_or_sections=[3,7,11]) # v表示竖直，行切片，行分割  v: axis=0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.array([0,1,2,3]*3)\n",
    "arr1.sort() # 排序，从小到大\n",
    "arr1 = arr1.reshape(4,3)\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr2 = np.array([1,2,3])\n",
    "display(arr1,arr2) # 形状不对应，依然可以进行运算：NumPy底层，为我们进行了广播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [2, 3, 4],\n",
       "       [3, 4, 5],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行不够，广播行\n",
    "arr1 + arr2 # arr2 和arr1 中每一行，进行相加：广播机制 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr3 = np.array([[1],[2],[3],[4]])\n",
    "display(arr1,arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [3, 3, 3],\n",
       "       [5, 5, 5],\n",
       "       [7, 7, 7]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列不够，广播列\n",
    "arr1 + arr3 # 广播，arr2和arr1中每一列，进行相加：广播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# 广播，和复制，意思有点近似"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]],\n",
       "\n",
       "       [[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]],\n",
       "\n",
       "       [[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([0,1,2,3,4,5,6,7]*3).reshape(3,4,2)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [2, 3],\n",
       "       [4, 5],\n",
       "       [6, 7]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([0,1,2,3,4,5,6,7]).reshape(4,2)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4, 2)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(4, 2)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(a.shape,b.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 0,  1],\n",
       "        [ 4,  9],\n",
       "        [16, 25],\n",
       "        [36, 49]],\n",
       "\n",
       "       [[ 0,  1],\n",
       "        [ 4,  9],\n",
       "        [16, 25],\n",
       "        [36, 49]],\n",
       "\n",
       "       [[ 0,  1],\n",
       "        [ 4,  9],\n",
       "        [16, 25],\n",
       "        [36, 49]]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * b # b形状(4，2),b 广播了三份"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 通用函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 元素级数字级别的方法\n",
    "矩陣廣播運算：\n",
    "- `abs`：絕對值\n",
    "- `sqrt`：平方根\n",
    "- `square`：平方\n",
    "- `exp`：自然底數e的多少次方\n",
    "- `log`：取自然對數\n",
    "- `sin`：取正弦值\n",
    "- `cos`：取餘弦值\n",
    "- `tan`：取正切值\n",
    "- `round`：取到小數點第幾位\n",
    "- `ceil`：向上取整數\n",
    "- `floor`：向下取整數\n",
    "- `maximum`：取最大值\n",
    "- `minimum`：取最小值\n",
    "- `all`：全部都 True 才會返回 True\n",
    "- `any`：只要有一個 True 就返回 True\n",
    "- `inner`：兩矩陣計算內積（對應位置相乘後總和）\n",
    "- `clip`：使得矩陣值落於範圍，小於下界強制變成下界；大於上界同理\n",
    "- `trace`：求矩陣對角線和\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "# abs、sqrt、square、exp、log、sin、cos、tan，maxinmum、minimum、\n",
    "# all、any、inner、clip、round、trace、ceil、floor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1, -3, -5,  1,  5,  8,  9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([-4, -1, -4, -5, -4,  6,  6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.array([-1,-3,-5,1,5,8,9])\n",
    "c = np.random.randint(-5,10,size = 7)\n",
    "display(a,c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5, 1, 5, 8, 9])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.abs(a) # 求绝对值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  4,  9, 16, 36, 49])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([1,4,9,16,36,49])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4., 6., 7.])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(b) # 开平方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1, -3, -5,  1,  5,  7, -3])"
      ]
     },
     "execution_count": 195,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.square(b) # 平方\n",
    "np.exp(3) # 自然底数e的多少次幂\n",
    "np.log(20.085536) # 自然底数e对数求解\n",
    "np.sin(np.pi/2) # 90度sin正弦值\n",
    "np.cos(0) # 余弦值\n",
    "np.tan(np.pi/6) # 正切,30度正切值\n",
    "# 给两个数组，从中选取大的，或者选取小的\n",
    "np.maximum(a,c) # 从a和c中选取最大的值\n",
    "np.minimum(a,c) # 选取最小的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 0])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([-1, -3,  4,  8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "nd1 = np.array([1,3,0]) # 出现0那么对应False，非零True\n",
    "nd2 = np.array([-1,-3,4,8])\n",
    "display(nd1,nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 198,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1.any() # 只要有一个True，返回True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 199,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1.all() # 所有True，返回True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 200,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2.all() # 所有的都是True，返回True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60"
      ]
     },
     "execution_count": 205,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([1,2,3,4,5])\n",
    "b = np.array([1,2,3,4,6])\n",
    "np.inner(a,b) # 1*1 + 2*2 + 3*3 + 4*4 + 5*6 = 60\n",
    "# 返回的是两个数组的内积，对应位置相乘加和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 206,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([17, 33, 69, 70, 81, 58, 47, 79, 60, 16, 34,  1, 72, 23, 27, 64, 73,\n",
       "       92, 85, 68, 39, 11,  7,  1, 33, 25, 90, 93, 55, 32])"
      ]
     },
     "execution_count": 206,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd1 = np.random.randint(0,100,size = 30)\n",
    "nd1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([17, 33, 69, 70, 80, 58, 47, 79, 60, 16, 34, 10, 72, 23, 27, 64, 73,\n",
       "       80, 80, 68, 39, 11, 10, 10, 33, 25, 80, 80, 55, 32])"
      ]
     },
     "execution_count": 207,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.clip(nd1,10,80) # 数据裁剪，将小于10变成10，将大于80的变成80"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.26615843, -0.02467659,  1.0457321 ,  1.6096884 ,  0.66723395,\n",
       "       -1.97988073,  0.67701791,  1.68360384, -1.19737442,  1.11051263,\n",
       "        0.75534365,  0.0654962 , -0.00653493, -0.14014643,  1.00227454,\n",
       "        1.26102407,  0.13945976, -0.97260932,  1.24669075, -0.35828001])"
      ]
     },
     "execution_count": 208,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2  = np.random.randn(20)\n",
    "nd2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 209,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.27, -0.02,  1.05,  1.61,  0.67, -1.98,  0.68,  1.68, -1.2 ,\n",
       "        1.11,  0.76,  0.07, -0.01, -0.14,  1.  ,  1.26,  0.14, -0.97,\n",
       "        1.25, -0.36])"
      ]
     },
     "execution_count": 209,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd2.round(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 212,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3., 3., 3.])"
      ]
     },
     "execution_count": 212,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ceil(np.array([2.7,2.1,2.05])) # 天花板，向上取整"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2., 2., 2.])"
      ]
     },
     "execution_count": 213,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.floor(np.array([2.99,2.9999,2.1])) # 向下取整"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 7, 6],\n",
       "       [9, 3, 7],\n",
       "       [8, 6, 1]])"
      ]
     },
     "execution_count": 220,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,10,size = (3,3))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 221,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.trace(a) # 就算对角线上的和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### where函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "nd1 = np.array([1,3,5,7,9])\n",
    "nd2 = np.array([2,4,6,8,10])\n",
    "cond = np.array([True,False,False,True,True])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4, 6, 7, 9])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(cond,nd1,nd2) # 条件如果是True，那么返回nd1中数据，如果Flase返回nd2中数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([43, 92, 26, 85, 38, 91, 63, 30, 14, 38, 27,  6, 17, 58,  0, 21, 50,\n",
       "       47, 46, 23, 57, 58, 18, 81, 87,  8, 86, 47,  4, 19, 95, 19, 40,  7,\n",
       "       14, 96, 87, 89, 90, 65, 12,  1, 76, 59, 65, 82, 57, 83, 25, 13])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([63, 92, 46, 85, 58, 91, 63, 50, 34, 58, 47, 26, 37, 58, 20, 41, 70,\n",
       "       67, 66, 43, 57, 58, 38, 81, 87, 28, 86, 67, 24, 39, 95, 39, 60, 27,\n",
       "       34, 96, 87, 89, 90, 65, 32, 21, 76, 59, 65, 82, 57, 83, 45, 33])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,100,size = 50)\n",
    "display(a) # 数据展示\n",
    "np.where(a > 50,a,a + 20) # 大于50，返回多少；不然返回-100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([84, 70, 37,  8, 74,  2, 94, 12, 76, 53, 49, 42, 36, 65, 81, 98, 69,\n",
       "       30,  7, 14, 61, 33, 81, 69, 50, 91, 10, 89, 66, 25, 80, 28, 57,  5,\n",
       "       18, 45, 85,  5, 70,  9, 64, 25, 38, 29, 10, 69, 36, 53, 57, 88])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果分数50~59分之间，自动加10分\n",
    "a = np.random.randint(0,100,size = 50)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([84, 70, 37,  8, 74,  2, 94, 12, 76, 63, 49, 42, 36, 65, 81, 98, 69,\n",
       "       30,  7, 14, 61, 33, 81, 69, 60, 91, 10, 89, 66, 25, 80, 28, 67,  5,\n",
       "       18, 45, 85,  5, 70,  9, 64, 25, 38, 29, 10, 69, 36, 63, 67, 88])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond = (a >=50) & (a < 60) # 与运算\n",
    "np.where(cond,a + 10,a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([22, 49, 25, 30, 10, 62, 57,  5, 61, 76, 97,  3, 60, 25, 22, 35, 11,\n",
       "        9, 71, 52])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,100,size = 20)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3,  5,  9, 10, 11, 22, 22, 25, 25, 30, 35, 49, 52, 57, 60, 61, 62,\n",
       "       71, 76, 97])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.sort(a) # 打印输出，那么，原数组，没有改变\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "a.sort() # 没有输出，说明，原数组上进行了排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3,  5,  9, 10, 11, 22, 22, 25, 25, 30, 35, 49, 52, 57, 60, 61, 62,\n",
       "       71, 76, 97])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,\n",
       "       17, 18, 19])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "index = a.argsort() # 返回排序的索引\n",
    "display(index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([97, 76, 71, 62, 61, 60, 57, 52, 49, 35, 30, 25, 25, 22, 22, 11, 10,\n",
       "        9,  5,  3])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据索引，花式索引\n",
    "a[index][::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### 集合操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 8, 23, 15, 21, 13, 11, 15,  7, 11, 22,  4, 28,  1, 18, 21])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([13, 22, 23, 19, 12, 27,  3,  0, 13, 22,  2,  8, 24, 10, 13])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(0,30,size = 15)\n",
    "b = np.random.randint(0,30,size = 15)\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 8, 13, 22, 23])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.intersect1d(a,b) # 交集：a和b中都有"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  1,  2,  3,  4,  7,  8, 10, 11, 12, 13, 15, 18, 19, 21, 22, 23,\n",
       "       24, 27, 28])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.union1d(a,b) # 并集:a和b中的所有，合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  4,  7, 11, 15, 18, 21, 28])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.setdiff1d(a,b) # 差集，a中有，b中没有"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "### 数学和统计函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "min、max、mean、median、sum、std、var、cumsum、cumprod、argmin、argmax、argwhere、cov、corrcoef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[73, 23,  5, 84, 13],\n",
       "       [ 2, 27, 52, 35,  0],\n",
       "       [96, 56, 20, 26, 10]])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randint(0,100,size = (3,5))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true,
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([92, 97, 76, 30, 81])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.max(axis = 0) # axis 轴，方向。axis = 0 行，axis = 1列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([76, 97, 48])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.max(axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "44.93333333333333"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.mean() # 平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26.0"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# a.median() # 矩陣不能直接用 .median()\n",
    "np.median(a) # 中位数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "674"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.sum() # 求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "29.588736595761123"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.std() # 标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "875.4933333333335"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.var() # 方差，数据内部波动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 73,  96, 101, 185, 198, 200, 227, 279, 314, 314, 410, 466, 486,\n",
       "       512, 522])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.cumsum() # 累计和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   1,    2,    6,   24,  120,  720, 5040])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([1,2,3,4,5,6,7])\n",
    "b.cumprod() # 累乘和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.argmin() # 返回最小值的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.argmax() # 返回最大值的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[73, 23,  5, 84, 13],\n",
       "       [ 2, 27, 52, 35,  0],\n",
       "       [96, 56, 20, 26, 10]])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0],\n",
       "       [0, 2],\n",
       "       [0, 3],\n",
       "       [0, 4],\n",
       "       [1, 0],\n",
       "       [1, 2],\n",
       "       [1, 4],\n",
       "       [2, 0],\n",
       "       [2, 1],\n",
       "       [2, 4]])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = np.argwhere((a > 50) | (a < 20)) # 返回就是符合条件的索引\n",
    "index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "73\n",
      "5\n",
      "84\n",
      "13\n",
      "2\n",
      "52\n",
      "0\n",
      "96\n",
      "56\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "for i,j in index:\n",
    "    print(a[i,j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1316.8 , -156.65,  618.3 ],\n",
       "       [-156.65,  492.7 , -292.9 ],\n",
       "       [ 618.3 , -292.9 , 1218.8 ]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# cov 协方差（属性之间进行计算），方差概念类似（数据内部，属性内部计算）\n",
    "# 举例子：一个男生受女生欢迎程度，和这名男生萎缩程度，是否成正比，什么关系\n",
    "np.cov(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        , -0.19448191,  0.48805989],\n",
       "       [-0.19448191,  1.        , -0.37797395],\n",
       "       [ 0.48805989, -0.37797395,  1.        ]])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.corrcoef(a) # 相关性系数，1（正相关） ~ -1（负相关）\n",
    "# 0 表示，没有线性关系"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## 线性代数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 7, 3],\n",
       "       [3, 5, 1],\n",
       "       [1, 2, 3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[6, 6, 2, 6],\n",
       "       [4, 9, 0, 4],\n",
       "       [7, 0, 5, 9]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "A = np.random.randint(0,10,size = (3,3))\n",
    "\n",
    "B = np.random.randint(0,10,size = (3,4))\n",
    "display(A,B) # A矩阵列长度必须和B行长度一致，不然报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[85, 99, 27, 91],\n",
       "       [45, 63, 11, 47],\n",
       "       [35, 24, 17, 41]])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.dot(B) # dot矩阵乘法 ，点乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[85, 99, 27, 91],\n",
       "       [45, 63, 11, 47],\n",
       "       [35, 24, 17, 41]])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(A,B) # 模块提供的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[85, 99, 27, 91],\n",
       "       [45, 63, 11, 47],\n",
       "       [35, 24, 17, 41]])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A @ B # 邮件中@符号，表示矩阵运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 7, 0, 9, 3],\n",
       "       [3, 7, 5, 7, 7],\n",
       "       [3, 2, 1, 6, 8],\n",
       "       [3, 4, 2, 6, 9]])"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = np.random.randint(0,10,size = (4,5))\n",
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "shapes (3,3) and (4,5) not aligned: 3 (dim 1) != 4 (dim 0)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-96-f96e5078725a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mA\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mC\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 形状不对应，无法进行矩阵乘法\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: shapes (3,3) and (4,5) not aligned: 3 (dim 1) != 4 (dim 0)"
     ]
    }
   ],
   "source": [
    "A.dot(C) # 形状不对应，无法进行矩阵乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 58, 132,  52,  67, 122, 113,  54,  41,  63,  71],\n",
       "       [123, 183, 107,  98, 183, 154,  73,  96, 129, 106],\n",
       "       [111, 137,  91,  67, 125, 131,  53,  67,  98,  79],\n",
       "       [124, 164, 108,  82, 153, 149,  62,  83, 112,  91]])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D = np.random.randint(0, 10, size = (5, 10))\n",
    "C.dot(D)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 42, 112,  44, 144, 130],\n",
       "       [ 39, 107,  53, 123, 111],\n",
       "       [ 42,  95,  23, 147, 142]])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# B.shape = (3,4);C.shape = (4,5)\n",
    "B.dot(C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "hidden": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "shapes (4,5) and (3,4) not aligned: 5 (dim 1) != 3 (dim 0)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-101-0ff241fbe642>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# C.shape = (4,5);B.shape = (3,4)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mC\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mB\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# 矩阵乘法不满足交换律！！！\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: shapes (4,5) and (3,4) not aligned: 5 (dim 1) != 3 (dim 0)"
     ]
    }
   ],
   "source": [
    "# C.shape = (4,5);B.shape = (3,4)\n",
    "C.dot(B) # 矩阵乘法不满足交换律！！！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## <font color = red>实战</font>-用NumPy分析鸢尾花花萼属性各项指标"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "案列：读取iris数据集中的花萼长度数据（已保存为csv格式）  \n",
    "并对其进行排序、去重，并求出和、累积和、均值、标准差、方差、最小值、最大值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5.1, 4.9, 4.7, 4.6, 5. , 5.4, 4.6, 5. , 4.4, 4.9, 5.4, 4.8, 4.8,\n",
       "       4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4.6, 5.1, 4.8, 5. ,\n",
       "       5. , 5.2, 5.2, 4.7, 4.8, 5.4, 5.2, 5.5, 4.9, 5. , 5.5, 4.9, 4.4,\n",
       "       5.1, 5. , 4.5, 4.4, 5. , 5.1, 4.8, 5.1, 4.6, 5.3, 5. , 7. , 6.4,\n",
       "       6.9, 5.5, 6.5, 5.7, 6.3, 4.9, 6.6, 5.2, 5. , 5.9, 6. , 6.1, 5.6,\n",
       "       6.7, 5.6, 5.8, 6.2, 5.6, 5.9, 6.1, 6.3, 6.1, 6.4, 6.6, 6.8, 6.7,\n",
       "       6. , 5.7, 5.5, 5.5, 5.8, 6. , 5.4, 6. , 6.7, 6.3, 5.6, 5.5, 5.5,\n",
       "       6.1, 5.8, 5. , 5.6, 5.7, 5.7, 6.2, 5.1, 5.7, 6.3, 5.8, 7.1, 6.3,\n",
       "       6.5, 7.6, 4.9, 7.3, 6.7, 7.2, 6.5, 6.4, 6.8, 5.7, 5.8, 6.4, 6.5,\n",
       "       7.7, 7.7, 6. , 6.9, 5.6, 7.7, 6.3, 6.7, 7.2, 6.2, 6.1, 6.4, 7.2,\n",
       "       7.4, 7.9, 6.4, 6.3, 6.1, 7.7, 6.3, 6.4, 6. , 6.9, 6.7, 6.9, 5.8,\n",
       "       6.8, 6.7, 6.7, 6.3, 6.5, 6.2, 5.9])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris = np.loadtxt('./iris.csv') # 花萼长度\n",
    "iris"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.3, 4.4, 4.4, 4.4, 4.5, 4.6, 4.6, 4.6, 4.6, 4.7, 4.7, 4.8, 4.8,\n",
       "       4.8, 4.8, 4.8, 4.9, 4.9, 4.9, 4.9, 4.9, 4.9, 5. , 5. , 5. , 5. ,\n",
       "       5. , 5. , 5. , 5. , 5. , 5. , 5.1, 5.1, 5.1, 5.1, 5.1, 5.1, 5.1,\n",
       "       5.1, 5.1, 5.2, 5.2, 5.2, 5.2, 5.3, 5.4, 5.4, 5.4, 5.4, 5.4, 5.4,\n",
       "       5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.5, 5.6, 5.6, 5.6, 5.6, 5.6, 5.6,\n",
       "       5.7, 5.7, 5.7, 5.7, 5.7, 5.7, 5.7, 5.7, 5.8, 5.8, 5.8, 5.8, 5.8,\n",
       "       5.8, 5.8, 5.9, 5.9, 5.9, 6. , 6. , 6. , 6. , 6. , 6. , 6.1, 6.1,\n",
       "       6.1, 6.1, 6.1, 6.1, 6.2, 6.2, 6.2, 6.2, 6.3, 6.3, 6.3, 6.3, 6.3,\n",
       "       6.3, 6.3, 6.3, 6.3, 6.4, 6.4, 6.4, 6.4, 6.4, 6.4, 6.4, 6.5, 6.5,\n",
       "       6.5, 6.5, 6.5, 6.6, 6.6, 6.7, 6.7, 6.7, 6.7, 6.7, 6.7, 6.7, 6.7,\n",
       "       6.8, 6.8, 6.8, 6.9, 6.9, 6.9, 6.9, 7. , 7.1, 7.2, 7.2, 7.2, 7.3,\n",
       "       7.4, 7.6, 7.7, 7.7, 7.7, 7.7, 7.9])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris = np.sort(iris) # 排序操作\n",
    "iris"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, 5.2, 5.3, 5.4, 5.5,\n",
       "       5.6, 5.7, 5.8, 5.9, 6. , 6.1, 6.2, 6.3, 6.4, 6.5, 6.6, 6.7, 6.8,\n",
       "       6.9, 7. , 7.1, 7.2, 7.3, 7.4, 7.6, 7.7, 7.9])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 去除重复数据\n",
    "iris = np.unique(iris)\n",
    "iris"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "210.39999999999998"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(iris) # 求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.011428571428571"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(iris) # 求平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  4.3,   8.7,  13.2,  17.8,  22.5,  27.3,  32.2,  37.2,  42.3,\n",
       "        47.5,  52.8,  58.2,  63.7,  69.3,  75. ,  80.8,  86.7,  92.7,\n",
       "        98.8, 105. , 111.3, 117.7, 124.2, 130.8, 137.5, 144.3, 151.2,\n",
       "       158.2, 165.3, 172.5, 179.8, 187.2, 194.8, 202.5, 210.4])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cumsum(iris) # 累加和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.0"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.median(iris) # 中位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0587265306122449"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.var(iris) # 方差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0289443768310533"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(iris) # 标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.3"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " iris.min() # 最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.9"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iris.max() # 最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.9"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(iris)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
